<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <!-- 所属分类 -->
      <el-form-item label="上级分类" prop="parent_id">
        <el-select style="width:225px;" v-model="ruleForm.parent_id" placeholder="顶级分类">
          <el-option
            v-for="catagoryList in catagoryList"
            :key="catagoryList.id"
            :label="catagoryList.name"
            :value="catagoryList.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- 商品名称 -->
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="ruleForm.name" style="width:225px;"></el-input>
      </el-form-item>
      <!-- 商品图片 -->
      <el-form-item label="分类图片" prop="wap_banner_url">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadImg"
        >
          <img v-if="ruleForm.wap_banner_url" :src="ruleForm.wap_banner_url" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <!-- 显示 -->
      <el-form-item label="显示" prop="is_show">
        <el-switch v-model="ruleForm.is_show"></el-switch>
      </el-form-item>
      <!-- 排序 -->
      <el-form-item label="排序" prop="sort_order">
        <el-input v-model="ruleForm.sort_order" style="width:225px;"></el-input>
      </el-form-item>
      <!-- 分类描述 -->
      <el-form-item label="分类描述" prop="front_desc">
        <el-input type="textarea" v-model="ruleForm.front_desc" style="width:225px;"></el-input>
      </el-form-item>
      <!-- 确认保存 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">确认保存</el-button>
        <el-button @click="cancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { PostCategoryInfo, UploadImgApi, GetTopCategory } from '../../../request/api'
export default {
  data() {
    return {
      catagoryList: [],
      headers: {
        'X-Nideshop-Token': localStorage.getItem('token')
      },
      ruleForm: {
        wap_banner_url: '',
        parent_id: 0,
        name: '',
        front_desc: '',
        is_show: false,
        sort_order: '',
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
        ],
        parent_id: [
          { required: true, message: '请选择活动区域', trigger: 'change' }
        ],
        front_desc: [
          { required: true, message: '品牌描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {

  },
  methods: {
    // 上传一张图片
    uploadImg(item) {
      console.log("item", item)
      let ruleForm = new FormData()
      ruleForm.append('good_pic', item.file)     //
      UploadImgApi(ruleForm).then(res => {
        console.log("返回图片路径", res.data)
        if (res.errno === 0) {
          this.ruleForm.wap_banner_url = res.data.fileUrl
        }
      })
    },
    // 商品图片
    handleAvatarSuccess(response, file, fileList) {
      //   console.log("response", response, file, fileList)
      this.wap_banner_url = response.data.fileUrl;
    },
    beforeAvatarUpload(file, fileList) {
      //   console.log("上传文件前的钩子", file, fileList);
    },

    // 
    submitForm(formName) {
      console.log("this.ruleForm", this.ruleForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          PostCategoryInfo({
            parent_id: this.ruleForm.parent_id,
            front_desc: this.ruleForm.front_desc,
            is_show: this.ruleForm.is_show,
            name: this.ruleForm.name,
            wap_banner_url: this.ruleForm.wap_banner_url,
            sort_order: this.ruleForm.sort_order,
          }).then(res => {
            console.log("提交成功", res)
            // this.$router.go(-1)
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    // 点击取消
    cancle() {
      console.log("点击了取消")
      this.$router.go(-1)
    }

  },
  created() {
    //   分类接口
    GetTopCategory({
      headers: this.headers
    }).then(res => {
      console.log("分类接口", res.data)
      if (res.errno == 0) {
        res.data.unshift({
          id: 0, name: "顶级分类", parent_id: 0
        })
        this.catagoryList = res.data
      }
    })
  }
}
</script>

<style lang='less' scoped>
.avatar-uploader .el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 105px;
  height: 105px;
  line-height: 105px;
  text-align: center;
  border: 1px solid #dcdfe6;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>